<?php

/**
 * @Author: wuchenge
 * @Date: 2020-12-07 16:40:01
 * @email: ligui0506@sina.com
 * @descript: 角色控制器
 */

namespace App\Admin\v1\Rbac;

use App\Http\Controllers\ApiController;
use App\Libraries\Utils;
use App\Models\Rbac\Role;
use Illuminate\Http\Request;

class RoleController extends ApiController
{
    public function lists(Request $request)
    {
        $field = ['id', 'name'];
        $limit = $request->input('size', config('api.commmon.size'));
        $page  = $request->input('page', config('api.commmon.page'));
        // 名称
        $name = $request->input('name', '');

        $where = [
            'nomal' => [],
        ];

        if (!empty($name)) {
            array_push($where['nomal'], [
                'name', 'like', $name . '%',
            ]);
        }

        $order = [];
        $with  = ['nodes', 'admins'];

        $result = Role::lists($field, $where, $page, $limit, $order, $with);

        return $this->response($result);
    }

    public function modify(Request $request)
    {
        $name = $request->input('name', '');
        $id   = $request->input('id', 0);
        $node = $request->input('node', []);

        if (empty($id)) {
            // 新增
            $res = Role::store($name, $node);
        } else {
            // 修改
            $res = Role::modify($id, $name, $node);
        }

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    public function delete(Request $request)
    {
        $id  = $request->input('id');
        $ids = explode(',', $id);
        $res = Role::del($ids);
        if ($res) {
            return $this->response();
        }
        return $this->failed();
    }
}
